package com.zzf.contactstest

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.net.Uri
import android.util.Log

class MyProvider : ContentProvider() {

    private val table1Dir = 0
    private val table1Item = 1
    private val table2Dir = 2
    private val table2Item = 3

//    private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH)

    val uriMatcher by later {

        Log.d("TAG","懒加载代码执行")

        val matcher = UriMatcher(UriMatcher.NO_MATCH)
        matcher.addURI("com.zzf.contactstest.provider","table1",table1Dir)
        matcher.addURI("com.zzf.contactstest.provider","table1/#",table1Item)
        matcher.addURI("com.zzf.contactstest.provider","table2",table2Dir)
        matcher.addURI("com.zzf.contactstest.provider","table2/#",table2Item)
        matcher
    }

//    init {
//        uriMatcher.addURI("com.zzf.contactstest.provider","table1",table1Dir)
//        uriMatcher.addURI("com.zzf.contactstest.provider","table1/#",table1Item)
//        uriMatcher.addURI("com.zzf.contactstest.provider","table2",table2Dir)
//        uriMatcher.addURI("com.zzf.contactstest.provider","table2/#",table2Item)
//    }

    override fun onCreate(): Boolean {
        TODO("Not yet implemented")
    }

    override fun query(
        uri: Uri,
        projection: Array<out String>?,
        selection: String?,
        selectionArgs: Array<out String>?,
        sortOrder: String?
    ): Cursor? {
        when(uriMatcher.match(uri)){
            table1Dir->{

            }
            table1Item->{

            }
            table2Dir->{

            }
            table2Item->{

            }
        }
        return null
    }

    override fun getType(uri: Uri): String? {
        return when (uriMatcher.match(uri)) {
            table1Dir -> "vnd.android.cursor.dir/vnd.com.zzf.contactstest.provider.table1"
            table1Item -> "vnd.android.cursor.dir/vnd.com.zzf.contactstest.provider.table1"
            table2Dir -> "vnd.android.cursor.dir/vnd.com.zzf.contactstest.provider.table2"
            table2Item -> "vnd.android.cursor.dir/vnd.com.zzf.contactstest.provider.table2"


            else -> null

        }
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
        return null
    }

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int {
        return 0
    }

    override fun update(
        uri: Uri,
        values: ContentValues?,
        selection: String?,
        selectionArgs: Array<out String>?
    ): Int {
        return 0
    }
}